package com.example.testanimation.objectanimation;

import java.util.ArrayList;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.example.testanimation.R;

public class ObjectAnimationLoadingActivity extends Activity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.animation_cloning);
		ViewGroup container = (ViewGroup) findViewById(R.id.container);
		final MyView child = new MyView(this);
		container.addView(child);
		findViewById(R.id.startButton).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				child.startAnimation();
			}
		});
	}
	
	
	class MyView extends View {

		AnimatorSet animationSet;
		
		ArrayList<ShapeHolder> sharps = new ArrayList<ShapeHolder>();
		
		public MyView(Context context) {
			super(context);
			
			sharps.add(new ShapeHolder().setX(100).setY(0));
			sharps.add(new ShapeHolder().setX(200).setY(0));
			sharps.add(new ShapeHolder().setX(300).setY(0));
			sharps.add(new ShapeHolder().setX(400).setY(0));
		}
		
		@SuppressLint("NewApi")
		public void startAnimation() {
			createAnimation();
			
			animationSet.start();
		}

		@SuppressLint("NewApi")
		private void createAnimation() {
			if(animationSet == null){
				ObjectAnimator ani0 = ObjectAnimator.ofFloat(sharps.get(0), "y", 0,getHeight() - sharps.get(0).getHeight()).setDuration(500);
				ObjectAnimator ani1 = ani0.clone();
				ani1.setTarget(sharps.get(1));
				
				ObjectAnimator animDown = ObjectAnimator.ofFloat(sharps.get(2), "y", 0f,getHeight() - sharps.get(0).getHeight()).setDuration(500);
				ObjectAnimator animUp = ObjectAnimator.ofFloat(sharps.get(2), "y", getHeight() - sharps.get(0).getHeight(),0f).setDuration(500);
				
				AnimatorSet s1 = new AnimatorSet();
				s1.playSequentially(animDown,animUp);
				AnimatorSet s2 = s1.clone();
				s2.setTarget(sharps.get(3));
				ObjectAnimator animColor = ObjectAnimator.ofInt(sharps.get(0), "color", Color.RED,Color.BLUE).setDuration(500);
				
				ValueAnimator fade = ValueAnimator.ofFloat(1f,0f);
				fade.addUpdateListener(new AnimatorUpdateListener() {
					
					@Override
					public void onAnimationUpdate(ValueAnimator animation) {
						Object value =  animation.getAnimatedValue();
						System.out.println("================== " + value);
						sharps.get(1).setAlpha((Float) animation.getAnimatedValue());
					}
				});
				
				s2.addListener(new AnimatorListener() {
					@Override
					public void onAnimationStart(Animator animation) {
						
					}
					
					@Override
					public void onAnimationRepeat(Animator animation) {
						
					}
					
					@Override
					public void onAnimationEnd(Animator animation) {
						startAnimation();
					}
					
					@Override
					public void onAnimationCancel(Animator animation) {
						
					}
				});
				animationSet = new AnimatorSet();
				animationSet.playTogether(ani0,ani1,s1,animColor,fade);
				animationSet.playSequentially(s1,s2);

			}
		}

		@Override
		protected void onDraw(Canvas canvas) {
			for (ShapeHolder sharp : sharps) {
				canvas.save();
				canvas.translate(sharp.getX(), sharp.getY());
				sharp.getShape().draw(canvas);
				canvas.restore();
			}
			invalidate();
		}
		
	}
	
}


